﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

using FXnaEngine.Components;

using Microsoft.Xna.Framework.Input;



namespace FXnaEngine
{

   /// <summary>
   /// 提供全方位的场景管理能力，并管理粒子，灯光，水等所有子系统的维护
   /// 要求包含鼠标拾取功能
   /// </summary>
   public class FSceneManger:BaseManager
    {


       public FSceneManger(FGame game)
           : base(game)
       {

           //场景的初始化？

           this._mainScene = new FScene(this.Game);

           this.Game.GraphicsDeviceService.DeviceReset += new EventHandler<EventArgs>(GraphicsDeviceService_DeviceReset);




 
       }

       void GraphicsDeviceService_DeviceReset(object sender, EventArgs e)
       {

          FCameraComponent  cam=  this._mainScene.RootEntity.GetComponentByType(typeof(FCameraComponent)) as FCameraComponent;


          cam.AspectRatio = (float)this.Game.GraphicsDevice.Viewport.Width / (float)this.Game.GraphicsDevice.Viewport.Height;




       }



        FScene _mainScene;

        public FScene MainScene
        {
            get { return _mainScene; }
           
        }




        /// <summary>
        /// Name of this manager
        /// </summary>
        public string Name
        {
            get { return name; }
        }
        private const string name = "SceneManager";






        protected override void InitializeCore()
        {
            base.InitializeCore();

          


        }

        protected override void UpdateCore(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.UpdateCore(gameTime);

            this._mainScene.UpdateCore(gameTime);


        }



        #region 内部功能函数



        /**//// <summary>
       /// 取得射线
       /// </summary>
        /// <returns></returns>
        private Ray GetRay()
        {
            //MouseState ms = Mouse.GetState();
            //Vector3 neerSource = new Vector3(ms.X, ms.Y, 0);
            //Vector3 farSource = new Vector3(ms.X, ms.Y, 1);

            //Vector3 neerPosi =FEngineState.Device.Viewport.Unproject(neerSource, projection, view, Matrix.Identity);
            //Vector3 farPosi = FEngineState.Device.Viewport.Unproject(farSource, projection, view, Matrix.Identity);
            //Vector3 direction=farPosi-neerPosi;
            //direction.Normalize();
            //return new Ray(neerPosi, direction);


            return new Ray(Vector3.Zero,Vector3.One);


        }



       /// <summary>
       /// 鼠标拾取
       /// </summary>
        protected void SelectEntity()
        {

             //Ray ray = GetRay();
             //  for (int i=0;i<models.Length;i++)
             //   {
             //        BoundingSphere bs=models[i].mod.Meshes[0].BoundingSphere;
             //      bs.Center = models[i].position;
             //       Nullable<float> result = ray.Intersects(bs);
             //      if (result.HasValue)
             //        {
             //            selectIndex = i;
             //        }
             //   }

 

        }



        #endregion

        #region 对外功能函数


        /// <summary>
       /// 取得场景的主渲染相机
       /// </summary>
       /// <returns></returns>
        public FEntity GetMainCameraEntity()
        {

            return this._mainScene.RootEntity;



 
        }







        #endregion

      





     





        /// <summary>
        /// Loads all needed ContentManager content.
        /// </summary>
        public void LoadContent(bool isReload, ContentManager contentManager, bool fromCompositor)
        {



          

         

            this.MainScene.LoadContent ();





        }

        /// <summary>
        /// Unloads all ContentManager content.
        /// </summary>
        public void UnloadContent()
        {


        }


    }
}
